// pages/register/register.js
var http = require('../../utils/http')
var util = require('../../utils/util.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    registerStep: 1, // 注册步骤
    errorTips: 0, // 输入错误提示:  1手机号输入错误  2验证码输入错误  3账号输入错误  4密码输入错误  5验证密码输入错误
    mobile: '',  //手机号
    validCode: '',  //验证码
    userName: '',  //用户名
    password: '',  //密码
    confirmPwd: '',   //确认密码
    checkRegisterSmsFlag: '', // 校验登陆注册验证码成功的标识

    // 验证码相关
    show: true,
    count: '',
    timer: null,
    hadGotCode: false,
    isResting: false,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },


  /**
   * 输入框的值
   */
  getInputVal: function (e) {
    const type = e.currentTarget.dataset.type
    if (type == 'mobile') {
      this.setData({
        mobile: e.detail.value
      })
    } else if (type == 'validCode') {
      this.setData({
        validCode: e.detail.value
      })
    } else if (type == "userName") {
      this.setData({
        userName: e.detail.value
      })
    } else if (type == "password") {
      this.setData({
        password: e.detail.value
      })
    } else if (type == "confirmPwd") {
      this.setData({
        confirmPwd: e.detail.value
      })
    }
  },


  /**
   * 获取验证码
   */
  getCode: function () {
    if (!util.checkPhoneNumber(this.data.mobile)) {
      this.setData({
        errorTips: 1
      })
      return
    }
    if (this.data.hadGotCode) {
      return
    }
    this.setData({
      hadGotCode: true,
      errorTips: 0
    })
    console.log('获取验证码')
    var params = {
      url: "/user/sendRegisterSms",
      method: "put",
      data: {
        mobile: this.data.mobile,
      },
      callBack: res => {
        this.setData({
          hadGotCode: true
        })
        const timeCount = 60;
        if (!this.data.timer) {
          let count = timeCount
          let timer = this.data.timer
          setTimeout(() => {
            this.setData({
              count: count,
              show: false
            })
          }, 1000)
          timer = setInterval(() => {
            if (count > 0 && count <= timeCount) {
              this.setData({
                count: count--
              })
            } else {
              clearInterval(timer);
              this.setData({
                timer: null,
                show: true,
                hadGotCode: false
              })
            }
          }, 1000)
        }
      }
    };
    http.request(params);
  },

  /**
   * 注册-下一步按钮
   */
  registerNext: function () {
    if (this.data.registerStep == 1) {
      console.log('第一步')
      if (!util.checkPhoneNumber(this.data.mobile)) {
        console.log('手机号出错')
        this.setData({
          errorTips: 1
        })
        return
      }
      else if (!this.data.validCode.trim()) {
        console.log(this.data.validCode)
        this.setData({
          errorTips: 2
        })
        return
      } else {
        console.log('校验验证码')
        this.setData({
          errorTips: 0
        })
        // 校验验证码
        var params = {
          url: "/user/checkRegisterSms",
          method: "put",
          data: {
            mobile: this.data.mobile,
            validCode: this.data.validCode
          },
          callBack: res => {
            let registerStep = this.data.registerStep
            registerStep += 1
            this.setData({
              registerStep,
              checkRegisterSmsFlag: res
            })
            console.log(this.data.registerStep)
          },
        };
        http.request(params);
        // let registerStep = this.registerStep += 1;
        // this.registerStep = registerStep
      }
    } else if (this.data.registerStep == 2) {
      console.log('第二步')
      if (!util.checkUserName(this.data.userName)) {
        this.setData({
          errorTips: 3
        })
        return
      } else if (!this.data.password.trim()) {
        this.setData({
          errorTips: 4
        })
        return
      } else if (this.data.password != this.data.confirmPwd) {
        this.setData({
          errorTips: 5
        })
        return
      } else {
        this.setData({
          errorTips: 0
        })
        wx.showLoading()
        console.log('请求注册接口')
        // 请求注册
        var params = {
          url: "/user/registerOrBindUser",
          method: "put",
          data: {
            appType: 1, // 应用类型 1小程序 2微信公众号 3 PC 4 H5
            checkRegisterSmsFlag: this.data.checkRegisterSmsFlag, // 校验登陆注册验证码成功的标识
            mobile: this.data.mobile,
            userName: this.data.userName,
            password: this.data.password,
            validateType: 1,  //  验证类型 1验证码验证 2 小程序encryptedData验证 3 密码验证
            registerOrBind:1 // 验证类型 1注册 2绑定
          },
          callBack: res => {
            wx.hideLoading()
            let registerStep = this.data.registerStep
            registerStep += 1
            this.setData({
              registerStep
            })
            wx.setStorageSync('loginResult', res)
            wx.setStorageSync('token', 'bearer' + res.access_token);
          }
        };
        http.request(params);
        console.log('注册成功')

      }
    }
  },

  /**
   * 去登陆
   */
  toLogin: function() {
    console.log('去登录')
    wx.navigateTo({
      url: "/pages/accountLogin/accountLogin"
    })
  },

  /**
   * 回到首页
   */
  toIndex: function () {
    wx.switchTab({
      url: '/pages/index/index'
    });
  },


  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})